// Tube8.js for PSPTube
// Made By JK108

function Tube8_CheckURL( url, option )
{
	if (url.match("http://www.tube8.com/") == "http://www.tube8.com/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Tube8 URL
*/
function Tube8_GetURL( url, option )
{
	// get video file URL
	var contents = GetContents( url );
	if(contents == null) { return null; /* error getting page */ }

	var pos = contents.indexOf("var videourl=\"");
	contents = contents.substring(pos + 14, contents.length);
	pos = contents.indexOf("\";");
	var video_url = contents.substring(0, pos);

	return video_url;
}

function Tube8_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;


	result.start = (((result.start - 1) / 10) * 24) + 1;


	if(result.start < 24){
	var contents = GetContents( "http://www.tube8.com/search.html?q=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / 24);
	var contents = GetContents( "http://www.tube8.com/search.html?q=" + keyword.replace(/ /g, "+") + "&page=" + num + "/" );
	}
	if(contents == null) { return null; /* failed to get page */ }


	result.total = 10000;


	while(1){
	var pos = contents.indexOf("<td colspan=\"2\" valign=\"top\" style=\"padding-bottom: 3px;\"><a href=\"");

	if(pos != -1){
		//Get URL
		contents = contents.substring(pos + 67, contents.length);
		pos = contents.indexOf("\"><span");
		var url = contents.substring(0, pos);
		//Thumbnail
		pos = contents.indexOf("<img width=\"160\" height=\"120\" src=\"");
		contents = contents.substring(pos + 35, contents.length);
		pos = contents.indexOf("\" alt=");
		var thumb_url = contents.substring(0, pos);
		//Title
		contents = contents.substring(pos + 7, contents.length);
		pos = contents.indexOf("\" id=");
		var title = ImproveText(contents.substring(0, pos));
		//Length
		pos = contents.indexOf("<td class=\"tinyInfo\"><b>");
		contents = contents.substring(pos + 24, contents.length);
		pos = contents.indexOf("</b>");
		var length = contents.substring(0, pos);
		var length = TimestringToSeconds(length);
		//Views
		pos = contents.indexOf("<td style=\"width: 100px;\">");
		contents = contents.substring(pos + 26, contents.length);
		pos = contents.indexOf(" views</td>");
		var views = parseInt(contents.substring(0, pos));
		//Description
		pos = contents.indexOf("<td>From: ");
		contents = contents.substring(pos + 10, contents.length);
		pos = contents.indexOf("</td>");
		var description = contents.substring(0, pos);
		pos = contents.indexOf("<td align=\"right\">");
		contents = contents.substring(pos + 18, contents.length);
		pos = contents.indexOf("</td>");
		description = "Added: " + contents.substring(0, pos) + " By: " + description;

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.ViewCount = views;
		info.Description = description;
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end   = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != 24){result.total = result.end;}

	return result;
}

// Service metadata
var Tube8 = new Object();
Tube8.Name          = "Tube8";		/* Service name */
Tube8.Description   = "Tube8";		/* Service description */
Tube8.SearchDesc    = "Tube8";		/* Service search screen description */
Tube8.SearchOSKMode = 1;		/* OSK mode 1: English only  2: Japanese */
Tube8.CheckURL      = Tube8_CheckURL;	/* CheckURL function */
Tube8.GetURL        = Tube8_GetURL;	/* GetURL function */
Tube8.Search        = Tube8_Search;	/* Search function */

// add YouTube to site list
SiteList.push( Tube8 );
